#ifndef __LINE_H__
#define __LINE_H__
#include "point.h"
#include "log.h"
template <typename T>
struct Line
{
    Point<T> start, end;
    T bottom, top, left, right;
    double A, B, C;

    Line() = default;
    Line(Point<T> s, Point<T> e) : start(s), end(e)
    {
        update_edges();
    }
    void update_edges()
    {
        log("Before update (%d,%d)->(%d,%d) \n", start.x, start.y, end.x, end.y);
        right = this->start.x < this->end.x ? this->end.x : this->start.x;
        left = this->start.x < this->end.x ? this->start.x : this->end.x;
        top = this->start.y < this->end.y ? this->end.y : this->start.y;
        bottom = this->start.y < this->end.y ? this->start.y : this->end.y;
        if (start == end)
        {
            A = B = C = 0;
        }
        else
        {
            A = end.y - start.y;
            B = start.x - end.x;
            C = -(A * start.x + B * start.y);
        }
        log_m("A= %f, B=%f, C=%f \n", A, B, C);
    }
    Point<T> get_point_at_x(T x)
    {
        log_m("got  x = %d A= %f, B=%f, C=%f \n", x, A, B, C);
        Point<T> p;
        if (A == 0)
        {
            // 平行于x轴
            p.x = x;
            p.y = start.y;
        }
        else if (B == 0)
        {
            // 平行于y轴
            if (x == start.x)
            {
                p.x = x;
                p.y = start.y;
            }
            else
            {
                // 破坏点
                p.x = x + 1;
            }
        }
        else
        {
            p.x = x;
            p.y = -(C + A * x) / B;
        }
        log("cross_point :(%d,%d)\n", p.x, p.y);

        return p;
    }
    Point<T> get_point_at_y(T y)
    {
        log("got  y = %d A= %f, B=%f, C=%f \n", y, A, B, C);
        Point<T> p;
        if (A == 0)
        {
            // 平行于x轴
            if (y == start.y)
            {
                p.y = y;
                p.x = start.x;
            }
            else
            {
                // 破坏点
                p.y = y + 1;
            }
        }
        else if (B == 0)
        {
            // 平行于y轴
            p.y = y;
            p.x = start.x;
        }
        else
        {
            p.y = y;
            p.x = -(C + B * y) / A;
        }
        log("cross_point :(%d,%d)\n", p.x, p.y);
        return p;
    }
    // // 外矩形的边界
    // T &get_bottom()
    // {
    //     return this->start.y < this->end.y ? this->start.y : this->end.y;
    // }
    // T &get_top()
    // {
    //     return this->start.y < this->end.y ? this->end.y : this->start.y;
    // }
    // T &get_left()
    // {
    //     return this->start.x < this->end.x ? this->start.x : this->end.x;
    // }
    // T &get_right()
    // {
    //     return this->start.x < this->end.x ? this->end.x : this->start.x;
    // }
    void draw()
    {
        glBegin(GL_LINES);
        glVertex2i(start.x, start.y);
        glVertex2i(end.x, end.y);
        glEnd();
    }
    // 仅支持L2平行X轴的情况
    Point<T> *crossWith(Line<T> &l2)
    {
        return nullptr;
    }
};

#endif //  __LINE_H__